package com.meihuak.cloud_ordering.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.meihuak.cloud_ordering.bean.Canteen;
import com.meihuak.cloud_ordering.bean.Dish;
import com.meihuak.cloud_ordering.bean.Business;
import com.meihuak.cloud_ordering.bean.OrderTable;
import com.meihuak.cloud_ordering.service.*;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.*;

@Slf4j
@Controller
@RequestMapping("/window")
public class BusinessController {


    @Autowired CanteenService canteenService;
    @Autowired BusinessService businessService;
    @Autowired DishService dishService;
    @Autowired
    OrderService orderService;
    @Autowired
    ManagerService managerService;
    @ResponseBody
    @RequestMapping("/getwininfo")
    public Map<String,Object> getAllInfo(@RequestParam(value = "id")int id){
        Business business = businessService.getBusinessById(id);
        Canteen canteen = canteenService.getById(business.getCanteenId());
        List<Dish> dishList = dishService.getDishByBusinessId(id);
        return winInfos(business,canteen,dishList,id);
    }

    @ResponseBody
    @RequestMapping("/login")
    public Map<String,Object> getAllInfo(@RequestParam(value = "id")int id,
                                         @RequestParam(value="password")String pwd,
                                         HttpSession session){
        Map<String,Object> resultMap = new HashMap<>();
        if(businessService.getBussinessByIdAndPwd(id,pwd)){
            session.setAttribute("id",id);
            resultMap.put("msg","登陆成功");
            resultMap.put("id",id);
            return resultMap;
        }
        resultMap.put("msg","登录失败");
        return resultMap;
    }

    @ResponseBody
    @RequestMapping("/getsortwin")
    public Map<String,Object> getAllInfo(@RequestParam(value = "id")int id,
                                         @RequestParam(value = "name")String name){
        Map<String,Object> resultMap = new HashMap<>();
        List<Business> list =  businessService.listSort(id,name);
        resultMap.put("msg","查询成功");
        resultMap.put("info",list);
        return resultMap;
    }



    public Map<String,Object> winInfos(Business business,Canteen canteen,List<Dish> dishList,int id){
        int waitnum = businessService.getWaitStudentCountByBusinessId(business.getId());
        int[] arr = new int[]{0,0,0,0,0};
        List<OrderTable> orderTables = orderService.getWinAllOrderByBid(business.getId());
        Date d = new Date();

//        for (int i =1; i <= orderTables.size(); i++) {
//            int tag = (int)(d.getTime()-orderTables.get(i-1).getCreateDate().getTime())/1000;//单位秒
//            for(int j=0;j<arr.length&&();j++){
//                arr[j]+=1;
//            }
//        }


        //将查询到的数据进行封装
        Map<String,Object> resultMap = new HashMap<>();
        Map<String,Object> info = new HashMap<>();
        info.put("id",id);
        info.put("winid", business.getNum());
        info.put("winname", business.getName());
        info.put("canid",canteen.getId());
        info.put("canname",canteen.getName());
        info.put("waitnum",waitnum);
        info.put("blackboard",business.getBlackboard());
        info.put("menu",dishList);
        info.put("arrNum",arr);

        resultMap.put("msg","查询完毕");
        resultMap.put("info",info);
        return resultMap;
    }


    @RequestMapping("/addbusiness")
    public ModelAndView addBusiness(@RequestParam(value = "cid")int cid,
                              @RequestParam(value = "winid")int num,
                              @RequestParam(value = "bname")String name,
                              @RequestParam(value = "bkey" ,required = false)String bkey,
                                    HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null&&(!businessService.numIsExist(num))){
            businessService.save(new Business(null,"icon/moren.jpg",num,name,cid,
                    "新店开业啦",99,0,5.0,bkey,12));
            System.out.println("已经进入后端");

            return getInfo(cid,session);


        }
        m.setViewName("manager/error");
        return m;

    }

    public ModelAndView getInfo(Integer cid,
                                             HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.getById(uid)!=null){
            List<Business> businesses = businessService.listByCanteenId(cid);
            modelAndView.setViewName("manager/window/canteeninfo");
            modelAndView.addObject("businesses",businesses);
            modelAndView.addObject("cid",cid);
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;
    }


    @RequestMapping("/addbypage")
    public ModelAndView getaddPage(@RequestParam(value = "cid")int cid,
                            HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            Canteen canteen = canteenService.getById(cid);
            m.addObject("canteen",canteen);
            m.setViewName("manager/add/addbusinessinfo");
            return m;
        }
        m.setViewName("manager/error");
        return m;


    }

    @Autowired CanteenController canteenController;

    @RequestMapping("/deldishinfo")
    public ModelAndView deldishinfo(@RequestParam(value = "bid")int bid,
                                    HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            int cid = businessService.getById(bid).getCanteenId();
            businessService.removeById(bid);
            return getInfo(cid,session);
        }
        m.setViewName("manager/error");
        return m;
    }


    @RequestMapping("/getalldish")
    public ModelAndView getAllDishbyBusinessId(@RequestParam(value = "bid")int bid,
                                   HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            List<Dish> dishes = dishService.getDishByBusinessId(bid);
            m.addObject("bid",bid);
            m.addObject("dishes",dishes);
            m.setViewName("manager/窗口菜品信息1");
            return m;
        }
        m.setViewName("manager/error");
        return m;


    }


    @RequestMapping("/adddish")
    public ModelAndView adddish(@RequestParam(value = "bid")int bid,
                                @RequestParam(value = "name")String name,
                                @RequestParam(value = "price")double price,
                                @RequestParam(value = "time")int time,
                                HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            Dish dish = new Dish();
            dish.setIcon("icon/1.jpg");
            dish.setFinishtime(time);
            dish.setName(name);
            dish.setPrice(price);
            dish.setWinId(bid);
            dish.setRecommendNum(0);
            dishService.save(dish);

            List<Dish> dishes = dishService.getDishByBusinessId(bid);
            m.addObject("dishes",dishes);
            m.setViewName("manager/窗口菜品信息1");
            return m;
        }
        m.setViewName("manager/error");
        return m;

    }

    @RequestMapping("/getadddishpage")
    public ModelAndView getadddishpage(@RequestParam(value = "bid")int bid,
                                        HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            m.addObject("bid",bid);

            m.setViewName("manager/adddish");
            return m;
        }
        m.setViewName("manager/error");
        return m;


    }


    @RequestMapping("/deldish")
    public ModelAndView deldish(@RequestParam (value = "did")int did,
                                HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            int bid = dishService.getById(did).getWinId();
            dishService.removeById(did);

            List<Dish> dishes = dishService.getDishByBusinessId(bid);
            m.addObject("dishes",dishes);
            m.setViewName("manager/窗口菜品信息1");
            return m;
        }
        m.setViewName("manager/error");
        return m;

    }


    @RequestMapping("/updateinfo")
    public ModelAndView updateinfo(@RequestParam (value = "bid")int bid,
                                   HttpSession session){

        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            Business business = businessService.getBusinessById(bid);
            m.addObject("business",business);
            m.setViewName("manager/updatebusiness");
            return m;
        }
        m.setViewName("manager/error");
        return m;
    }

    @RequestMapping("/updatebusinessinfo")
    public ModelAndView updatebusinessinfo(@RequestParam (value = "cid")int cid,
                                            @RequestParam (value = "bid")int bid,
                                           @RequestParam (value = "num")int num,
                                           @RequestParam (value = "bname")String bname,
                                           @RequestParam (value = "bkey")String bkey,
                                           HttpSession session){

        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            UpdateWrapper<Business> updateWrapper = new UpdateWrapper<>();
            updateWrapper.set("num",num)
                        .set("name",bname)
                        .set("userkey",bkey)
                        .eq("id",bid);
            businessService.update(updateWrapper);
            List<Business> businesses = businessService.listByCanteenId(cid);

            m.addObject("businesses",businesses);
            m.addObject("cid",cid);
            m.setViewName("manager/window/canteeninfo");
            return m;
        }
        m.setViewName("manager/error");
        return m;

    }

    @RequestMapping("/updatedish")
    public ModelAndView updatedish(@RequestParam(value = "did")int did,
                                   HttpSession session){
        Integer uid = (int)session.getAttribute("id");
        ModelAndView modelAndView = new ModelAndView();
        if(managerService.getById(uid)!=null){
            Dish dish = dishService.getById(did);
            modelAndView.setViewName("manager/updatedish");
            modelAndView.addObject("dish",dish);
            return modelAndView;
        }
        modelAndView.setViewName("manager/error");
        return modelAndView;
    }



    @RequestMapping("/updatedishinfo")
    public ModelAndView updatedish(@RequestParam(value = "bid")int bid,
                                    @RequestParam(value = "did")int did,
                                   @RequestParam(value = "name")String name,
                                   @RequestParam(value = "price")double price,
                                   @RequestParam(value = "time")int time,

                                   HttpSession session){

        Integer uid = (int)session.getAttribute("id");
        ModelAndView m = new ModelAndView();
        if(managerService.getById(uid)!=null){
            UpdateWrapper<Dish> updateWrapper = new UpdateWrapper<>();
            updateWrapper
                    .set("name",name)
                    .set("price",price)
                    .set("finishtime",time)
                    .eq("id",did);
            dishService.update(updateWrapper);
            List<Dish> dishes = dishService.getDishByBusinessId(bid);
            m.addObject("dishes",dishes);
            m.setViewName("manager/窗口菜品信息1");

            return m;
        }
        m.setViewName("manager/error");
        return m;

    }
}
